import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/extension/date_string_extension.dart';
import 'package:tiktokadult/hj_model/message/message_dynamic_list.dart';
import 'package:tiktokadult/hj_model/message/message_unread_list.dart';
import 'package:tiktokadult/hj_page/community_business/post_detail_page/post_detail_page.dart';
import 'package:tiktokadult/hj_page/mine_business/chat/widget/chat_logic.dart';
import 'package:tiktokadult/hj_page/mine_business/mine_utils.dart';
import 'package:tiktokadult/hj_page/user_center_page/user_center_page.dart';
import 'package:tiktokadult/hj_utils/date_time_util.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/global_store/store.dart';
import 'package:tiktokadult/tools_base/widget/net_image_widget.dart';

//私信
class MessageSessionItem extends StatefulWidget {
  final MessageUnreadList model;

  const MessageSessionItem(this.model, {super.key});

  @override
  State<MessageSessionItem> createState() => _MessageSessionItemState();
}

class _MessageSessionItemState extends State<MessageSessionItem> {
  MessageUnreadList get model => widget.model;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        final uid = globalStore.isMe(model.takeUid?.toInt()) ? model.sendUid?.toInt() : model.takeUid?.toInt();
        ChatLogic.goToChatPage(
          uid: uid ?? 0,
          userName: model.userName ?? '',
        );
        setState(() {
          model.noReadNum = 0;
        });
      },
      child: Row(
        children: [
          NetworkImageLoader(
            imageUrl: model.userAvatar ?? '',
            width: 48,
            height: 48,
            borderRadius: 24,
          ),
          12.sizeBoxW,
          Expanded(
              child: Column(
            children: [
              12.sizeBoxH,
              Row(
                children: [
                  Flexible(
                    child: Text(
                      model.userName ?? '',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),
                    ),
                  ),
                  if (model.noReadNum != 0) ...[
                    2.sizeBoxH,
                    Container(
                      padding: const EdgeInsets.symmetric(horizontal: 8),
                      decoration: BoxDecoration(
                        color: AppColors.primaryXColor,
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Text(
                        model.noReadNum.toString(),
                        style: textStyle(12, Colors.white, FontWeight.w400),
                        maxLines: 1,
                      ),
                    )
                  ],
                  6.sizeBoxW,
                  Text(
                    formatTime(model.createdAt),
                    style: TextStyle(color: Color(0xff525252), fontSize: 12, fontWeight: FontWeight.w400),
                  )
                ],
              ),
              2.sizeBoxH,
              Row(
                children: [
                  Expanded(
                    child: Text(
                      model.preContent ?? '',
                      style: TextStyle(
                          color: Color(0xff525252), fontSize: 14.sp, fontWeight: FontWeight.w400, overflow: TextOverflow.ellipsis),
                      maxLines: 1,
                    ),
                  ),
                ],
              ),
              10.sizeBoxH,
            ],
          ))
        ],
      ),
    );
  }
}

/// 点赞
class MessageThumbItem extends StatelessWidget {
  final MessageDynamicList model;
  const MessageThumbItem(this.model, {super.key});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () => Get.toNamed(
        PostDetailPage.routeName,
        arguments: PostDetailPageArgument(id: model.objId, models: []),
      ),
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 12, vertical: 12),
        margin: EdgeInsets.only(bottom: 12),
        decoration: BoxDecoration(
          color: Color(0x0DFFFFFF),
          borderRadius: BorderRadius.circular(12),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Expanded(
                  child: Text(
                    "#$lTypeText",
                    maxLines: 1,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 12,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
                SizedBox(width: 12),
                Text(
                  model.createdAt.utcTimeSToYMDString() ?? '',
                  style: TextStyle(
                    color: Color(0xE5FFFFFF),
                    fontSize: 14,
                  ),
                ),
              ],
            ),
            7.sizeBoxH,
            Text(
              '${model.objName}',
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: Color(0x8CFFFFFF),
                fontSize: 14,
              ),
            ),
            6.sizeBoxH,
            Row(
              children: [
                Text(
                  "${model.likeCount}人为你点赞",
                  style: TextStyle(
                    color: Color(0x59FFFFFF),
                    fontSize: 14,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  String get lTypeText {
    // video:对帖子点赞 comment:对评论点赞
    switch (model.msgType) {
      case 'like_msg':
        return '帖子点赞';
      case 'like_comment_msg':
        return '评论点赞';
      default:
        return '';
    }
  }
}

//点赞
class MessageCustomerServiceItem extends StatelessWidget {
  const MessageCustomerServiceItem({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        InkWell(
          onTap: () async => MineUtils.goCSPage(),
          child: Container(
            height: 60,
            padding: EdgeInsets.only(left: 16),
            child: Row(
              children: [
                Container(
                  width: 50,
                  height: 50,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(25),
                    gradient: LinearGradient(
                      colors: [
                        Color(0xff84A4F9),
                        Color(0xff2B5DDE),
                      ],
                    ),
                  ),
                  child: Image.asset(
                    'customer_service.png'.mineImgPath,
                    width: 60,
                  ),
                ),
                10.sizeBoxW,
                Text(
                  "官方客服",
                  style: TextStyle(color: Colors.white, fontSize: 14),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}

class MessageCommentItem extends StatelessWidget {
  final MessageDynamicList model;

  const MessageCommentItem(this.model, {super.key});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => Get.toNamed(
        PostDetailPage.routeName,
        arguments: PostDetailPageArgument(id: model.objId, models: []),
      ),
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 18, horizontal: 12),
        margin: EdgeInsets.only(bottom: 12),
        decoration: BoxDecoration(
          color: Color(0x0DFFFFFF),
          borderRadius: BorderRadius.circular(12),
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            InkWell(
              onTap: () => Get.toNamed(UserCenterPage.routeName, arguments: {'uid': model.sendUid ?? 0}),
              child: NetworkImageLoader(
                imageUrl: model.sendAvatar ?? '',
                width: 40,
                height: 40,
                borderRadius: 20,
              ),
            ),
            12.sizeBoxW,
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    height: 40,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          model.sendName ?? '',
                          style: TextStyle(
                            color: Color(0xFFFFFFFF),
                            fontSize: 14,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        Spacer(),
                        Text(
                          DateTimeUtil.utc2iso(model.createdAt),
                          style: TextStyle(
                            color: Color(0xFF989898),
                            fontSize: 12,
                          ),
                        ),
                      ],
                    ),
                  ),
                  11.sizeBoxH,
                  Text(
                    model.content ?? '',
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: Color(0xFF989898),
                      fontSize: 12,
                    ),
                  ),
                ],
              ),
            ),
            8.sizeBoxW,
            Container(
              alignment: Alignment.bottomCenter,
              height: 82,
              child: NetworkImageLoader(
                imageUrl: model.objCover ?? '',
                height: 60,
                width: 60,
                borderRadius: 6,
              ),
            )
          ],
        ),
      ),
    );
  }
}
